//    Copyright (C) Kherty.  All rights reserved.
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Input;

namespace OpenLS.UI
{
    public class GalleryItem : ListBoxItem
    {
        static GalleryItem()
        {
            DefaultStyleKeyProperty.OverrideMetadata(typeof (GalleryItem),
                                                     new FrameworkPropertyMetadata(typeof (GalleryItem)));
        }


        protected override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
        {
            if (!e.Handled)
            {
                CaptureMouse();
            }
            base.OnMouseLeftButtonDown(e);
        }


        protected override void OnMouseLeftButtonUp(MouseButtonEventArgs e)
        {
            if (IsMouseOver)
            {
                ReleaseMouseCapture();
                var gallery = (Gallery) (ItemsControl.ItemsControlFromItemContainer(this));
                if (IsMouseOver)
                {
                    if (gallery.SelectedItem == Content)
                        gallery.ClearValue(Selector.SelectedItemProperty);
                    gallery.SelectedItem = Content;
                }
                gallery.IsOpen = false;
            }
            base.OnMouseLeftButtonUp(e);
        }
    }
}